package assignment1.inheritance;

import java.util.Hashtable;

public class Class {

	private Class superClass; // superclass, if any
	public Hashtable<String, Function> dict; // function datastructure. should be private, but example is public
	public String name; // class name, used for testing superclass principle
	
	public Class(String nameInput) {
		this.dict = new Hashtable<String, Function>();
		this.name = nameInput;
	}
	
	// returns a new Class object, with current Class object as superclass
	public Class inherit(String nameInput) {
		Class subClass = new Class(nameInput);
		subClass.superClass = this;
		return subClass;
	}
	
	public Function searchFunc(String funcName) {
		System.out.println("Searching method " + funcName + " in: " + this.name);
		// search for method in current dict
		if (this.dict.containsKey(funcName)) {
			System.out.println(funcName + " found in " + this.name);
			return this.dict.get(funcName);
		} 
		// if not found, search in superclass
		else if (this.superClass != null) {
			System.out.println("Superclass found: " + superClass.name);
			return superClass.searchFunc(funcName);
		}
		return null; // if method is not found
	}
}
